﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Sexybeauty.Framework.Common;
using Sexybeauty.Framework.Configration;

namespace Sexybeauty.Core.ExServer.WorkPool.WorkHandle.Email
{
    internal abstract class BaseEmailHandle<T> : IWorkHandle<T>
    {
        string partition = string.Empty;
        protected virtual void Prepare(EmailContext ctx, string partition="account")
        {
            this.partition = partition;
            var serveritem = ConfigReader.WorkPool.EmailQueue.Pool[partition].ServerList.RandomGet();
            ctx.From = serveritem.Address;
            ctx.CredentialPassword = serveritem.Password;// System.Configuration.ConfigurationManager.AppSettings["EMail-Password"];
            ctx.CredentialUserName = serveritem.Username;// System.Configuration.ConfigurationManager.AppSettings["Email-UserName"];
        }
        protected void SendMailCallBack(EmailContext context)
        {
            if (ConfigReader.WorkPool.EmailQueue.Pool[this.partition].Enable)
            {
                new EMail(context).Send();
            }
        }

        #region IWorkHandle<T> 成员

        public abstract WaitWorkItem Process(T context);

        #endregion
    }

    #region Account相关
    /// <summary>
    /// 注册邮件
    /// </summary>
    internal class EmailForRegisterHandle<T> : BaseEmailHandle<T>
    {
        public override WaitWorkItem Process(T context)
        {
            AccountEmail emailctx = context as AccountEmail;
            WaitWorkItem data = new WaitWorkItem();
            #region 赋值Context对象
            var ctx = new EmailContext();
            ctx.Recipient = emailctx.Recipient;
            Prepare(ctx);
            ctx.Subject = SiteResource.SiteName+ "--账号激活邮件";
            ctx.DisplayName = SiteResource.SiteName;
            ctx.Body = BuildWelcomeEmailBody(emailctx.Recipient, emailctx.Token);
            data.Context = ctx;
            #endregion

            #region 赋值Action
            data.Action = o => { SendMailCallBack(o as EmailContext); };
            #endregion

            return data;
        }
        /// <summary>
        /// 构建注册激活邮件内容
        /// </summary>
        /// <param name="username"></param>
        /// <param name="token">注册时生成的token值</param>
        /// <returns></returns>
        public string BuildWelcomeEmailBody(string username, string fixtoken)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<style type=\"text/css\">");
            sb.AppendLine(".content{margin-left: auto;margin-right: auto;width: 700px;margin-top: 20px;line-height:160%;}");
            sb.AppendLine(".content-line{overflow: hidden;width: 100%;}");
            sb.AppendLine(".content-line-left{float: left;overflow: hidden;}");
            sb.AppendLine(".content-line-right{float: right;}");
            sb.AppendLine(".content-line-vspace{float: left;width: 15px;overflow: hidden;height: 1px;}");
            sb.AppendLine(".content-logo{}");
            sb.AppendLine(".content-topright{margin-top: 35px;margin-right: 5px;}");
            sb.AppendLine(".content-topright a{color: #333;text-decoration: none;}");
            sb.AppendLine(".content-topright span{color: #E1E1E1;padding: 0 5px;}");
            sb.AppendLine(".hline{overflow: hidden;height: 15px;width: 100%;margin-top: 4px;border-top: 1px solid #fafafa;}");
            sb.AppendLine(".content-toprighttext{padding-top: 15px;}");
            sb.AppendLine(".box{overflow: hidden;margin-left: auto;margin-right: auto;border-right: 1px solid #F0F6FA;border-bottom: 1px solid #F0F6FA;padding: 0 1px 1px 0;background-color: #E0ECF5;color: #4f4e4e;	padding-bottom: 1px;padding-right:1px;}");
            sb.AppendLine(".boxbg{background-color: #F9FCFD;border: 1px solid #BDC8D9;padding-left:20px;padding-right:20px;padding-top:10px;padding-bottom:20px;}");
            sb.AppendLine(".box-title{padding: 15px 10px 5px 10px;border-bottom: 1px solid #F3F3F3;margin-bottom: 5px;}");
            sb.AppendLine(".box-line{overflow: hidden;padding: 5px 10px 5px 10px;}");
            sb.AppendLine(".box-dotline{overflow: hidden;width: 755px;height: 5px;border-bottom: dotted 1px #b3b5b5;margin-left: auto;margin-right: auto;}");
            sb.AppendLine(".content-footer{border-top: 1px solid #F3F3F3;line-height: 1.8em;padding: 18px 0;color: #999;clear: both;margin-top: 20px;font-size:13px;}");
            sb.AppendLine(".content-footer-left{float: left;}");
            sb.AppendLine(".content-footer-left span{color: #999;padding: 0 5px;}");
            sb.AppendLine(".content-footer-left a{color: #666;text-decoration: none;}");
            sb.AppendLine(".content-footer-right{float: right;text-align: right;}");
            sb.AppendLine("</style>");
            sb.AppendLine("<div class=\"content\"><div class=\"content-line\"><div class=\"content-line-left\"><div class=\"content-logo\">");
            sb.AppendLine("<img src=\"http://www." + SiteResource.Domain + SiteResource.Logo + "\" /></div>");
            sb.AppendLine("</div><div class=\"content-line-right\"><div class=\"content-toprighttext\"></div>");
            sb.AppendLine("</div></div><div class=\"hline\"></div><div class=\"content-line\"><div class=\"box\"><div class=\"boxbg\">");
            sb.AppendLine("<p>尊敬的用户：" + username + "，<br/>您好！</p>");
            sb.AppendLine("<p>欢迎您注册"+SiteResource.SiteName+"，请点击以下链接激活您的账号</p>");
            sb.AppendLine("<p><a href=\"http://www." + SiteResource.Domain + "/account/activation_token=" + fixtoken + "\">http://www." +
                SiteResource.Domain + "/account/activation_token=" + fixtoken + "</a></p>");
            sb.AppendLine("</div></div></div><div class=\"content-line\"><div class=\"content-footer\"><div class=\"content-footer-left\">");
            sb.AppendLine("<a href=\"http://www." + SiteResource.Domain + "/home/about\" target=\"_blank\">关于我们</a><span>|</span><a href=\"http://www." + SiteResource.Domain + "/home/contact\" target=\"_blank\">联系我们</a><span>|</span>");
            sb.AppendLine("<a href=\"http://www." + SiteResource.Domain + "/home/business\" target=\"_blank\">商务合作</a><span>|</span><br/>"); 
            sb.AppendLine("地址：" + SiteResource.CompanyAddress + "&nbsp;&nbsp;电话：" + SiteResource.CompanyPhone);
            sb.AppendLine("</div></div></div></div>");
            return sb.ToString();
        }
    }
    internal class EmailForResetPwdHandle<T> : BaseEmailHandle<T>
    {
        public override WaitWorkItem Process(T context)
        {
            AccountEmail emailctx = context as AccountEmail;
            WaitWorkItem data = new WaitWorkItem();
            #region 赋值Context对象
            var ctx = new EmailContext();
            ctx.Recipient = emailctx.Recipient;
            Prepare(ctx);
            ctx.Subject = SiteResource.SiteName + "--密码重置验证";
            ctx.Body = BuildResetPasswordEmailBody(emailctx.Recipient, emailctx.Token);
            data.Context = ctx;
            #endregion

            #region 赋值Action
            data.Action = o => { SendMailCallBack(o as EmailContext); };
            #endregion

            return data;
        }
        public string BuildResetPasswordEmailBody(string username, string fixtoken)
        {
            string url = "http://" + SiteResource.Domain + "/account/verifyresetpassword_token=" + fixtoken;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<style type=\"text/css\">");
            sb.AppendLine(".content{margin-left: auto;margin-right: auto;width: 700px;margin-top: 20px;line-height:160%;}");
            sb.AppendLine(".content-line{overflow: hidden;width: 100%;}");
            sb.AppendLine(".content-line-left{float: left;overflow: hidden;}");
            sb.AppendLine(".content-line-right{float: right;}");
            sb.AppendLine(".content-line-vspace{float: left;width: 15px;overflow: hidden;height: 1px;}");
            sb.AppendLine(".content-logo{}");
            sb.AppendLine(".content-topright{margin-top: 35px;margin-right: 5px;}");
            sb.AppendLine(".content-topright a{color: #333;text-decoration: none;}");
            sb.AppendLine(".content-topright span{color: #E1E1E1;padding: 0 5px;}");
            sb.AppendLine(".hline{overflow: hidden;height: 15px;width: 100%;margin-top: 4px;border-top: 1px solid #fafafa;}");
            sb.AppendLine(".content-toprighttext{padding-top: 15px;}");
            sb.AppendLine(".box{overflow: hidden;margin-left: auto;margin-right: auto;border-right: 1px solid #F0F6FA;border-bottom: 1px solid #F0F6FA;padding: 0 1px 1px 0;background-color: #E0ECF5;color: #4f4e4e;	padding-bottom: 1px;padding-right:1px;}");
            sb.AppendLine(".boxbg{background-color: #F9FCFD;border: 1px solid #BDC8D9;padding-left:20px;padding-right:20px;padding-top:10px;padding-bottom:20px;}");
            sb.AppendLine(".box-title{padding: 15px 10px 5px 10px;border-bottom: 1px solid #F3F3F3;margin-bottom: 5px;}");
            sb.AppendLine(".box-line{overflow: hidden;padding: 5px 10px 5px 10px;}");
            sb.AppendLine(".box-dotline{overflow: hidden;width: 755px;height: 5px;border-bottom: dotted 1px #b3b5b5;margin-left: auto;margin-right: auto;}");
            sb.AppendLine(".content-footer{border-top: 1px solid #F3F3F3;line-height: 1.8em;padding: 18px 0;color: #999;clear: both;margin-top: 20px;font-size:13px;}");
            sb.AppendLine(".content-footer-left{float: left;}");
            sb.AppendLine(".content-footer-left span{color: #999;padding: 0 5px;}");
            sb.AppendLine(".content-footer-left a{color: #666;text-decoration: none;}");
            sb.AppendLine(".content-footer-right{float: right;text-align: right;}");
            sb.AppendLine("</style>");
            sb.AppendLine("<div class=\"content\"><div class=\"content-line\"><div class=\"content-line-left\"><div class=\"content-logo\">");
            sb.AppendLine("<img src=\"http://www." + SiteResource.Domain + SiteResource.Logo + "\" /></div>");
            sb.AppendLine("</div><div class=\"content-line-right\"><div class=\"content-toprighttext\"></div>");
            sb.AppendLine("</div></div><div class=\"hline\"></div><div class=\"content-line\"><div class=\"box\"><div class=\"boxbg\">");
            sb.AppendLine("<p>尊敬会员：" + username + "，<br/>您好！</p>");
            sb.AppendLine("<p>您在"+SiteResource.SiteName+"申请了重置密码。请点击以下链接以继续您的操作：</p>");
            sb.AppendLine("<p><a href=\"" + url + "\">" + url + "</a></p>");
            sb.AppendLine("<p>如果您未申请过重置密码，请尽快<a target=\"_blank\" href=\"http://www."+SiteResource.Domain+"/account/login\">登录"+SiteResource.SiteName+"</a>，并修改您的密码</p>");
            sb.AppendLine("</div></div></div><div class=\"content-line\"><div class=\"content-footer\"><div class=\"content-footer-left\">");
            sb.AppendLine("<a href=\"http://www." + SiteResource.Domain + "/home/about\" target=\"_blank\">关于我们</a><span>|</span><a href=\"http://www." + SiteResource.Domain + "/home/contact\" target=\"_blank\">联系我们</a><span>|</span>");
            sb.AppendLine("<a href=\"http://www." + SiteResource.Domain + "/home/business\" target=\"_blank\">商务合作</a><span>|</span><br/>"); 
            sb.AppendLine("地址：" + SiteResource.CompanyAddress + "&nbsp;&nbsp;电话：" + SiteResource.CompanyPhone);
            sb.AppendLine("</div></div></div></div>");
            return sb.ToString();
        }

    }
    #endregion
}
